﻿using CraftParameterConfig.Domain.Entity;
using CraftParameterConfig.Domain.Interfaces;
using CraftParameterConfig.Infrastructure.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CraftParameterConfig.Infrastructure.Data
{
    public class ParameterDataRespository : RespositoryBase, IParameterDataRespository
    {
        DbContext dbContext = GlobalObjects.DbContext;
        ObjectChange obc = GlobalObjects.ObjectChange;
        public ParameterDataRespository(IUnitOfWork unitOfWork)
                : base(unitOfWork)
        { }
        public void Add(ParameterDataValue entity)
        {
            string sql = GetInsertSql(entity);
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void AddRange(List<ParameterDataValue> entitys)
        {
            List<string> sql = new List<string>();
            foreach (var item in entitys)
            {
                sql.Add(GetInsertSql(item));
            }
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Delete(Guid id)
        {
            throw new Exception("表MDA_PARAMETER_DATA_VALUE不支持删除操作,如果有此需求请通过表MDA_PARAMETER_ACCESS进行操作");
        }

        public void Delete(ParameterDataValue entity)
        {
            throw new Exception("表MDA_PARAMETER_DATA_VALUE不支持删除操作,如果有此需求请通过表MDA_PARAMETER_ACCESS进行操作");
        }

        public void DeleteAll()
        {
            throw new Exception("表MDA_PARAMETER_DATA_VALUE不支持删除操作,如果有此需求请通过表MDA_PARAMETER_ACCESS进行操作");
        }

        public ParameterDataValue GetEntityByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            if (dtSource.Rows.Count != 1)
            {
                throw new Exception("当前条件在ParameterDataValue表中不能找到唯一一条记录,请重新设置条件再进行查询!");
            }
            return obc.ChangeTableToObject<ParameterDataValue>(dtSource).First();
        }

        public ParameterDataValue GetEntityByGuid(Guid id)
        {
            string condition = string.Format(" where MAINID='{0}'", id.ToString());
            return GetEntityByContition(condition);
        }

        public List<ParameterDataValue> GetEntitysByContition(string condition)
        {
            DataTable dtSource = GetTable(condition);
            return obc.ChangeTableToObject<ParameterDataValue>(dtSource).ToList();
        }

        public DataTable GetTable(string condition)
        {
            string sql = string.Format("select * from MDA_PARAMETER_DATA_VALUE {0}", condition);
            var con = dbContext.GetDbConnection();
            try
            {
                con.Open();
                return dbContext.GetTable(dbContext.GetDbCommand(con), sql);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public void Update(ParameterDataValue entity)
        {
            string sql = GetInsertSql(entity);
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }

        public void Update(List<ParameterDataValue> entitys)
        {
            List<string> sql = new List<string>();
            foreach (var item in entitys)
            {
                sql.Add(GetUpdateSql(item));
            }
            using (DbTransaction dbTransaction = _unitOfWork.BeginTransaction())
            {
                var com = dbContext.GetDbCommand(dbTransaction.Connection);
                dbContext.ExecuteSqlNoQuery(com, sql);
                _unitOfWork.SaveChange();
            }
        }
        private string GetInsertSql(ParameterDataValue entity)
        {
            return string.Format("Insert into MDA_PARAMETER_DATA_VALUE(MAINID,FIELDNAME,FIELDVALUE,CREATE_USER)values('{0}','{1}','{2}','SC')", entity.MainID, entity.FieldName, entity.FieldValue);
        }
        private string GetUpdateSql(ParameterDataValue entity)
        {
            return string.Format("Update MDA_PARAMETER_DATA_VALUE set FIELDVALUE='{0}' where MAINID='{1}' and FIELDNAME='{2}'", entity.FieldValue, entity.MainID, entity.FieldName);
        }
    }
}
